package com.dirox.plugins.webwork;

import static com.google.common.base.Preconditions.checkNotNull;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlClauseBuilder;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.web.action.JiraWebActionSupport;
import com.atlassian.jira.web.bean.PagerFilter;
import com.dirox.plugins.ao.entities.Note;
import com.dirox.plugins.ao.service.NoteService;

import java.util.*;

public class CreateNote extends JiraWebActionSupport {
	private final JiraAuthenticationContext context;
	private NoteService noteService;
	private UserManager userManager;
	private SearchService searchService;
	private ProjectManager projectManager;

	public CreateNote(JiraAuthenticationContext context,
			NoteService noteService, UserManager userManager,
			SearchService searchService,
			ProjectManager projectManager) {
		this.context = checkNotNull(context);
		this.noteService = checkNotNull(noteService);
		this.userManager = checkNotNull(userManager);
		this.searchService = checkNotNull(searchService);
		this.projectManager = checkNotNull(projectManager);
	}

	public List<Issue> getIssuesByProjectName(String projectName) {
		final User currentUser = context.getLoggedInUser();
		JqlClauseBuilder jqlClauseBuilder = JqlQueryBuilder.newClauseBuilder();
		com.atlassian.query.Query query = jqlClauseBuilder.project(projectName)
				.buildQuery();
		PagerFilter pagerFilter = PagerFilter.getUnlimitedFilter();
		com.atlassian.jira.issue.search.SearchResults searchResults = null;
		try {
			searchResults = searchService.search(currentUser, query,
					pagerFilter);
		} catch (SearchException e) {
			e.printStackTrace();
		}
		return searchResults.getIssues();
	}

	public List<Issue> getIssues() {
		List<Issue> issues = new ArrayList<Issue>();
		List<Project> projects = projectManager.getProjectObjects();
		for (Project p : projects) {
			issues.addAll(getIssuesByProjectName(p.getName()));
		}
		return issues;
	}
	
	public List<Project> getProjects() {
		return projectManager.getProjectObjects();
	}

	public List<User> getUsers() {
		final User currentUser = context.getLoggedInUser();

		List<User> users = new ArrayList<User>();
		users.addAll(userManager.getAllUsers());
		users.remove(currentUser);
		return users;
	}

	protected void doValidation() {
		log.debug("Entering doValidation");
		for (Enumeration e =  request.getParameterNames(); e.hasMoreElements() ;) {
			String n = (String)e.nextElement();
			String[] vals = request.getParameterValues(n);
			log.debug("name " + n + ": " + vals[0]);
		}
	}

	/**
	 * This method is always called when this Action's .jspa URL is
	 * invoked if there were no errors in doValidation().
	 */
	protected String doExecute() throws Exception {
		log.debug("Entering doExecute");
//		for (Enumeration e =  request.getParameterNames(); e.hasMoreElements() ;) {
//			String n = (String)e.nextElement();
//			String[] vals = request.getParameterValues(n);
//			log.debug("name " + n + ": " + vals[0]);
//		}
		return SUCCESS;
	}

	public String doDefault() throws Exception
    {
        log.debug("Entering doDefault");
        
        return SUCCESS;
    }

	private String aStringVariable = "a default value";

	/**
	 * This method is automatically discovered and called by JSP and Webwork
	 * if the name matches the id of a parameter passed in an HTML form.
	 * The class of the parameter (String) has to match, and the
	 * method has to be public or it is silently ignored.
	 */
	public void setMyfirstparameter(String value) {
		log.debug("Setting aStringVariable to: " + value);
		this.aStringVariable = value;
	}

	/**
	 * This is how the local variable is always accessed, since only this
	 * action knows that its name isn't really "myfirstparameter".
	 */
	public String getMyfirstparameter() {
		log.debug("Getting aStringVariable");
		return aStringVariable;
	}

}
